//
//  main.c
//  Signal
//
//  Created by wangrui on 15-3-12.
//  Copyright (c) 2015年 wangrui. All rights reserved.
//

#include <stdio.h>
#include <signal.h>
#include <stdlib.h>
#include <unistd.h>

void die () {
    printf("Good Bye Cruel World! \n");
    exit(1);
}

void timeUp() {
    printf("Time up! \n");
    exit(1);
}

int catch_signal(int sig, void ( *handler)(int) ) {
    struct sigaction action;
    action.sa_handler = handler;
    sigemptyset(&action.sa_mask);
    action.sa_flags = 0;
    
    return sigaction(sig, &action, NULL);
}

int main(int argc, const char * argv[]) {
    
    if (catch_signal(SIGINT, die) == -1) {
        printf("can map the handler \n");
        exit(1);
    }
    
    if (catch_signal(SIGALRM, timeUp) == -1) {
        printf("can map the handler \n");
        exit(1);
    }
    
    char name[30];
    printf("input: \n");
    scanf("%s",name); //终端运行 输入 Ctrl-C
    printf("%s \n",name);
    
    raise(SIGINT);// 可手动raise signal,终端看效果
    
    while (1) {
        alarm(2); //时间到发出sigal
        scanf("%s",name); //达到暂停程序的目的
    }
    
    return 0;
}
